<?php
/*
	Hide the auth stuff in here.  I can change it later when I figure it out.

	All objects in the Session must be serialized prior to storing.
	Lots more functions to come

After login, implement the cookie cache
cookie is u, it has 4 url encoded fields separated by #
version, "name", "level", "mode","baseUrl"
change clientcache.js to add more fields.
*/
class Veneer_Session
{
	protected $_veneer_parms;	// array of parms
	protected $_veneer_log;	// array of parms
	protected $_site_config;
	protected $_msite_config;
	protected $_auth;	// login
	protected $_dbAdapter;	// login
	protected $_registry;	// login
	protected $_debug;	// login

	protected $_session;
	protected $_sessionStartup;

	protected $_arr_user;
	protected $_session_user;

	function __construct()
	{
		$this->_registry = Zend_Registry::getInstance();
		$this->_msite_config = $this->_registry->get('msite_config');
		$this->_site_config = $this->_registry->get('site_config');

//		if(!isset(_COOKIE['u']))
//			setcookie('u','#########',time()+60*60*24*30*12);

	// Session
		$arr = $this->_site_config->session->toArray();
		$arr['save_path'] = CACHE_PATH . 'sessions';
		Zend_Session::setOptions($arr);
		Zend_Session::rememberMe(864000);
		Zend_Session::start();
		$this->_sessionStartup = new Zend_Session_Namespace('Veneer_Tracker');
		if (!isset($this->_sessionStartup->startTime)) {
			$this->setStartup();
		}

	}
	function setStartup()
	{
		$this->_sessionStartup->startTime = time();
		$this->_sessionStartup->ipAddr = $_SERVER['REMOTE_ADDR'];
		$this->_sessionStartup->browser = $_SERVER['HTTP_USER_AGENT'];
		if(isset($_SERVER['HTTP_REFERER']))
			$this->_sessionStartup->referer = $_SERVER['HTTP_REFERER'];
	}


	function loadRegistry()
	{
		$this->_dbAdapter = $this->_registry->get('dbAdapter');
		$this->_veneer_log = $this->_registry->get('veneerLog');
		$this->_debug = $this->_registry->get('veneer_debug');
	}
	function login( $user)	// authorize
	{
		// setup User Array
		$this->_arr_user = $user->toArray();
		unset($this->_arr_user['password']);
		unset($this->_arr_user['site_id']);
		//
		if($this->isLoggedIn()) {
			Zend_Session::expireSessionCookie();	// reset the session to get new one.
			$this->setStartup();	// reset as start of session
		}
		// Save to session
		$this->_session_user = new Zend_Session_Namespace('Veneer_User');
		$this->_session_user->userArr = serialize($this->_arr_user);
		// add other user info


//		$cache_cookie = "Alvin#a#test#/zend/veneer2#";
		$cache_cookie = "1#".str_replace ( '+', '%20', urlencode($this->_arr_user['real_name']))
			. '#'.$this->_arr_user['user_type']
			.'#test#'			//*********************** Fix msite variable
			. $this->_site_config->baseUrl. '#';
		print $cache_cookie;
		setrawcookie( 'u' , $cache_cookie , time() + 100000 , '/');

	}
	function genCookie()
	{ 	// Version 1
		// "version", "name", "level", "mode","baseUrl" 	alvin#a#test#/zend/veneer2#


	}
	function isLoggedIn()		// check cookie to see if user is authorized
	{
		$this->_session_user = new Zend_Session_Namespace('Veneer_User');
		if( isset($this->_session_user->userArr))
			return true;
		return false;
	}
	function logout()
	{
		if( strtolower($_SERVER['HTTP_HOST']) == 'localhost')
			setcookie ("u", "", time() - 3600,'/');
		else
			setcookie ("u", "", time() - 3600,'/',$_SERVER['HTTP_HOST']);
		Zend_Session::expireSessionCookie();
//        Zend_Auth::getInstance()->clearIdentity();
	}

	function printSession()
	{
		$this->_session_user = new Zend_Session_Namespace('Veneer_User');
		$this->_arr_user =unserialize($this->_session_user->userArr);
		$content = "<br/><br/><pre>" . print_r($this->_arr_user,true) ."</pre>";
		return $content;
	}

}